<template>
  <el-card>
    <el-button type="primary" @click="dialogVisible=true">添加角色</el-button>

    <el-table
      :data="tableData"
      style="width: 100%;margin: 20px 0"
      border
      stripe
    >
      <el-table-column
        width="40"
        align="center"
        type="expand"
      >
        <template slot-scope="{row}">
          <down-content :row="row" />
        </template>
      </el-table-column>
      <el-table-column
        type="index"
        label="#"
        width="40"
        align="center"
      />
      <el-table-column
        prop="roleName"
        label="角色名称"
      />
      <el-table-column
        prop="roleDesc"
        label="角色描述"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editRole(row)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
          <el-button size="mini" type="warning" icon="el-icon-s-tools" @click="editRights(row)">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    添加修改角色弹窗-->
    <Dialog v-if="dialogVisible" ref="dialogRole" v-model="dialogVisible" @success="loadRolesList" />
    <!--    修改权限弹窗-->
    <edit-right ref="rightDialog" v-model="rightVisible" @success="loadRolesList" />
  </el-card>
</template>

<script>
import { getRoles } from '@/api/roles'
import Dialog from '@/views/Roles/components/Dialog'
import DownContent from '@/views/Roles/components/DownContent'
import EditRight from '@/views/Roles/components/EditRight'
export default {
  components: { EditRight, DownContent, Dialog },
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      rightVisible: false
    }
  },

  created() {
    this.loadRolesList()
  },

  methods: {
    async loadRolesList() {
      this.tableData = await getRoles()
    },
    editRole(row) {
      this.dialogVisible = true
      this.$nextTick(_ => {
        this.$refs.dialogRole.form = { ...row }
      })
    },
    async editRights(row) {
      this.rightVisible = true
      await this.$refs.rightDialog.loadTreeData()
      this.$refs.rightDialog.setSelect(row.children)
      this.$refs.rightDialog.roleId = row.id
    }
  }
}
</script>

